package com.web.admin.web.base.open;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.http.message.ViewBuilder;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.open.facade.UrlFacade;
import com.web.base.open.message.UrlRequest;
import com.web.base.open.message.UrlResponse;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller("openUrlController")
@Http(url = "/base/open")
public class UrlController
{
	@Resource
	private UrlFacade urlFacade;

	private static DictParser dictParser = DictParser.getInstance();
	
	public UrlController()
	{
	}
	
	@Http(url = "/url_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		UrlRequest urlRequest = request.getQueryObject(UrlRequest.class);
		return View.createBuilder().setView("/base/open/url_list")
				.put("model", request)
				.put("treeList", urlFacade.listTree())
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/url_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		UrlRequest urlRequest = request.getQueryObject(UrlRequest.class);
		UrlResponse response = null;
		if (urlRequest.getId() != null)
		{
			response = urlFacade.get(urlRequest.getId());
			urlRequest.setParentId(response.getParentId());
		}
		ViewBuilder builder = View.createBuilder();
		if (urlRequest.getParentId() != null)
		{
			builder.put("parentModel", urlFacade.get(urlRequest.getParentId()));
		}
		return builder.setView("/base/open/url_get")
				.put("model", response == null ? new UrlResponse() : response)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/url_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		UrlRequest urlRequest = request.getPostObject(UrlRequest.class);
		UrlResponse response = null;
		if (urlRequest.getId() == null)
		{
			ValidationUtil.valid(urlRequest, SaveModel.class);
			response = urlFacade.add(urlRequest);
		}
		else
		{
			ValidationUtil.valid(urlRequest, UpdateModel.class);
			response = urlFacade.update(urlRequest);
		}
	}
	
	
}
